﻿using Leeder.Logger;
using SPSAndong.Comm;
using SPSAndong.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPSAndong.UserControll
{
    public partial class ExceptionCallShow : UserControl
    {
        /// <summary>
        /// 日志
        /// </summary>
        LogOperate LogObj => Log.GetLogger(this.GetType());
        IList<ProjectSetModel> list;
        /// <summary>
        /// 当前页数
        /// </summary>
        int _index = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        int _pages = 0;
        /// <summary>
        /// 安东展示线程控制
        /// </summary>
        CancellationTokenSource _cancelandongshow;
        /// <summary>
        /// 类型：1、2、3、4
        /// </summary>
        public int[] _type;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type"></param>
        public ExceptionCallShow(int[] type)
        {
            InitializeComponent();
            this._type = type;
            this.Disposed += ExceptionCallShow_Disposed;
            _cancelandongshow = new CancellationTokenSource();
            //开启安东展示控制线程
            Task.Factory.StartNew(() => { UpdateData(); }, _cancelandongshow.Token);
        }
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExceptionCallShow_Load(object sender, EventArgs e)
        {

        }
        private void UpdateData()
        {
            while (true)
            {
                if (this._cancelandongshow != null && this._cancelandongshow.IsCancellationRequested)
                {
                    this._cancelandongshow.Dispose();
                    break;
                }
                //查询要展示的数据
                var data = GolbalVariable.ProjectBag.Where(x => this._type.Contains(x.AreaType) &&x.State!=0).ToList();
                if (data == null || data.Count == 0)
                {
                    RefreshData(null);
                    //continue;
                }
                else if (data.Count <= 3)
                {
                    list = data;
                    RefreshData(list.OrderByDescending(x=>x.UpdateTime).ToList());
                }
                else
                {
                    if (this._index == this._pages)
                    {
                        this._pages = (data.Count % 3 == 0) ? (data.Count / 3) : ((data.Count / 3) + 1);
                        this._index = 0;
                        list = data;
                    }
                    var result = list.OrderByDescending(x => x.UpdateTime).Skip(this._index * 3).Take(3).ToList();
                    this._index++;
                    RefreshData(result);
                }
                Thread.Sleep(GolbalVariable.AppSet.AndongShowRefresh * 1000);
            }
        }
        /// <summary>
        /// led显示数据
        /// </summary>
        /// <param name="data"></param>
        public void RefreshData(IList<ProjectSetModel> list)
        {
            if (this.label0.InvokeRequired)
            {
                try
                {
                    this.Invoke(new MethodInvoker(() =>
                    {
                        UpdateUI(list);
                    }));
                }
                catch (Exception ex)
                {
                    LogObj.Error(ex.Message);
                }
            }
            else
                UpdateUI(list);

        }
        /// <summary>
        /// 更新UI数据
        /// </summary>
        private void UpdateUI(IList<ProjectSetModel> list)
        {
            label1.Text = "";
            label2.Text = "";
            label0.Text = "";
            if (list == null)
            {
                return;
            }
            for (int i = 0; i < list.Count; i++)
            {
                if (this.label0.Name.Contains(i.ToString()))
                {
                    this.label0.Text = list[i].WorkName;
                    if (list[i].State == 1)
                        this.label0.ForeColor = Color.Black;
                    else
                        this.label0.ForeColor = Color.Red;
                }
                else if (this.label1.Name.Contains(i.ToString()))
                {
                    this.label1.Text = list[i].WorkName;
                    if (list[i].State == 1)
                        this.label1.ForeColor = Color.Black;
                    else
                        this.label1.ForeColor = Color.Red;
                }
                else if (this.label2.Name.Contains(i.ToString()))
                {
                    this.label2.Text = list[i].WorkName;
                    if (list[i].State == 1)
                        this.label2.ForeColor = Color.Black;
                    else
                        this.label2.ForeColor = Color.Red;
                }
            }
        }
        /// <summary>
        /// /控件销毁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExceptionCallShow_Disposed(object sender, EventArgs e)
        {
            if (this._cancelandongshow != null & !this._cancelandongshow.IsCancellationRequested)
                this._cancelandongshow.Cancel();
        }
    }
}
